package com.wsn.smarthouse;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



import android.util.Log;
//import com.wsn.util.Log;

public class RecThread extends Thread implements Runnable {
	private DatagramSocket ds;
	private byte[] receiveData = new byte[4096];
	private String tag = "RecThread";
	private HashMap<String, String> sensorinfo;
	public RecThread( DatagramSocket ds, HashMap<String, String> sensorinfo){
		super();
		this.ds=ds;
		this.sensorinfo=sensorinfo;
	}
	
	@Override
	public void run(){
		while(true)
		{
			DatagramPacket rdp = new DatagramPacket(receiveData,
					receiveData.length);			
			try{
				ds.receive(rdp);
			}catch(SocketException e){
			
			}catch(IOException e){
				//import com.wsn.util.Log;
			}
			String result = new String(rdp.getData(), rdp.getOffset(), 
					rdp.getLength());
			Log.v(tag,""+result);
			if(result.startsWith("DATA:Get Info/Node")){	//Get the Sensor info
				String[] arrayString = result.split("\n");
				for (int i=1; i<arrayString.length; i++){
					String[] onelineSet = arrayString[i].split("\t");
					if(onelineSet.length>1){
						sensorinfo.put(onelineSet[0],onelineSet[2]);
					}
				}
			}else if(result.startsWith("EVENT")){
				String regEx="INFRAREDSCREEN";
    			Pattern p=Pattern.compile(regEx);
    			Matcher m=p.matcher(result);
    			boolean a=m.find();
    			if(a==true){
    				
    			}
    			
			}
		}
		
	}
}
